#!/bin/bash

file="$1"
line="$2"
debug="$3"

echo file: $file
echo line: $line
echo debug: $debug

for server in `mvim --serverlist` 
do
    if [[ $debug ]] ; then echo mvim --servername $server --remote-expr "WhichTab('$file')"; fi
    foundfile=`mvim --servername $server --remote-expr "WhichTab('$file')"`
    if [[ $foundfile > 0 ]]
    then
        mvim --servername $server --remote-expr "foreground()" 
        if [[ $debug ]] ; then echo mvim --servername $server --remote-send ":exec \"tabnext $foundfile\" <CR>"; fi
        mvim --servername $server --remote-send ":exec \"tabnext $foundfile\" <CR>"
        if [[ $debug ]] ; then echo mvim --servername $server --remote-send ":$line <CR>"; fi
        mvim --servername $server --remote-send ":$line <CR>"
    fi
done
